<?php
include "pgsql.class.php";

$objDB = new pgsql();
/*
 tid       | integer                | not null default nextval('trace_tid_seq'::regclass)
 uid       | bigint                 | not null
 tracetype | integer                | not null default 1
 hidecnt   | integer                | not null default 0
 title     | character varying(255) |

*/
$uid	= $_REQUEST['uid'];
$SQL = "SELECT a.tid, a.uid, a.tracetype, a.hidecnt, a.title, a.rdate, b.path FROM trace as a LEFT JOIN tracefile as b ON (a.tid = b.tid) WHERE a.uid = " . $uid . " order by a.tid desc limit 10";
/*
{"data" : [{"tid":"54","uid":"1","tracetype":"0","hidecnt":"0","title":"%EC%82%AC%EC%A7%84%EC%84%B8%EA%B0%9C","rdate":"2012-01-18 11:40:48.066913+09","path":"2_1_54_1326854448.jpg"},
*/
if($objDB->query($SQL)) {
	$res	= $objDB->fetch();
	$tmpCnt = count($res);
	$json = "{\"data\" : ";
	for($i=0;$i<$tmpCnt;$i++) {
		if($i == 0) {
			$tmpArray[$res[$i]['tid']] = $res[$i];
			$tmpArray2[$res[$i]['tid']]['path'][] = $res[$i]['path'];
		}
		if($res[$i]['tid'] != $res[$i-1]['tid'] && $i != 0) {
			$tmpArray[$res[$i]['tid']] = $res[$i];
			$tmpArray2[$res[$i]['tid']]['path'][] = $res[$i]['path'];
		} else if ($res[$i]['tid'] == $res[$i-1]['tid'] && $i != 0){
			$tmpArray2[$res[$i]['tid']]['path'][] = $res[$i]['path'];
		}
		/*
		if($res[$i]['tid'] != $res[$i]['tid'] && $i != 0) {
			$path = "\"path\":[";
			$json .= "[".
			$json .= "{\"tid\":\"" .$res[$i]['tid']. "\",\"uid\":\"" .$res[$i]['uid']. "\",\"tracetype\":\"" .$res[$i]['tracetype']. "\",";
			$json .= "\"hidecnt\":\"" .$res[$i]['hidecnt']. "\",\"title\":\"" .$res[$i]['title']. "\",\"rdate\":\"" .$res[$i]['rdate']. "\",";
			$path .= "\"" . $res[$i]['path'][$i] . "\",";
		} else {
			$path .= "\"" . $res[$i]['path'][$i] . "\",";
		}
		*/
	}
//	$res = $objDB->fetchJson();
	//print_r($tmpArray);
foreach($tmpArray as $key=>$value) {
	foreach($value as $keys=>$values) {
		if($keys == 'path') {
			$tmpArray[$key][$keys] = $tmpArray2[$key][$keys];
		}
	}
	//$tmpArray[$key][$value]
}
	$objJson = new json();
	$rtn = "{\"data\" : " . $objJson->encode($tmpArray) . "}";
	print_r($rtn);
} else {
	echo "{\"error\" : 0}";
}
?>